/**
 *
 */
package top.jle.jpa.basic.log;

import com.alibaba.fastjson2.JSONArray;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author jim.lee
 *
 */
@Aspect
@Component("logInterceptor")
public class LogInterceptor {

	private static final Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);

    @Around("@annotation(bussLog)")
    public Object around(ProceedingJoinPoint pjp,BussLog bussLog) throws Throwable{
        // 开始时间
        long startTimeMillis = System.currentTimeMillis();
    	String params = "";
        if (pjp.getArgs() !=  null && pjp.getArgs().length > 0) {
            try {
            	params = JSONArray.toJSONString(pjp.getArgs());
			} catch (Exception e) {
				params = Arrays.toString(pjp.getArgs());
			}
        }
		String targetName= pjp.getTarget().getClass().getName()+"=>"+pjp.getSignature().getName();
    	Object obj = pjp.proceed();
        // 结束时间
        long endTimeMillis = System.currentTimeMillis();
        Long duration = (endTimeMillis - startTimeMillis);
        LOGGER.info("调用方法:{} 描述:{} 传入参数:{} 耗时:{}ms",targetName,bussLog.description(),params,duration);
    	return obj;
    }
}
